package com.code.day20;

import java.io.*;
import java.util.ArrayList;

import static com.code.day20.Demo12Serializable.read;

public class Demo13JingYan {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
//        write(); //序列化
        read(); //反序列化
    }

    public static void write() throws IOException {
        ObjectOutputStream objectOutputStream =
                new ObjectOutputStream(new FileOutputStream("D:\\java-code\\java-full-stack\\01-java-base\\src\\com\\code\\day20\\file\\demo6.txt"));
        ArrayList<Person> list = new ArrayList<>();

        Person p1 = new Person("张三", 18);
        Person p2 = new Person("李四", 18);
        Person p3 = new Person("王五", 18);
        list.add(p1);
        list.add(p2);
        list.add(p3);
        objectOutputStream.writeObject(list);
        objectOutputStream.close();
    }

    public static void read() throws IOException, ClassNotFoundException {
        ObjectInputStream objectInputStream =
                new ObjectInputStream(new FileInputStream("D:\\java-code\\java-full-stack\\01-java-base\\src\\com\\code\\day20\\file\\demo6.txt"));
        Object o = objectInputStream.readObject();
        ArrayList<Person> list = (ArrayList<Person>) o;
        for (Person person : list) {
            System.out.println(person);
        }

        objectInputStream.close();
    }
}
